package com.sgj.gulimall.coupon.service.impl;

import com.sgj.common.to.SkuReductionTo;
import com.sgj.common.to.SkuReductionTo.*;
import com.sgj.gulimall.coupon.entity.MemberPriceEntity;
import com.sgj.gulimall.coupon.entity.SkuLadderEntity;
import com.sgj.gulimall.coupon.service.MemberPriceService;
import com.sgj.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgj.common.utils.PageUtils;
import com.sgj.common.utils.Query;

import com.sgj.gulimall.coupon.dao.SkuFullReductionDao;
import com.sgj.gulimall.coupon.entity.SkuFullReductionEntity;
import com.sgj.gulimall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // sms_sku_ladder
        SkuLadderEntity skuLadderEntity = SkuLadderEntity.builder()
                .skuId(skuReductionTo.getSkuId())
                .fullCount(skuReductionTo.getFullCount())
                .discount(skuReductionTo.getDiscount())
                .addOther(skuReductionTo.getCountStatus())
                .build();
        if (skuReductionTo.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }

        // sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(BigDecimal.ZERO) == 1) {
            this.save(skuFullReductionEntity);
        }

        // 会员价格
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();

        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
            MemberPriceEntity priceEntity = MemberPriceEntity.builder()
                    .skuId(skuReductionTo.getSkuId())
                    .memberLevelId(item.getId())
                    .memberLevelName(item.getName())
                    .memberPrice(item.getPrice())
                    .addOther(1)
                    .build();
            return priceEntity;
        }).filter(item -> item.getMemberPrice().compareTo(BigDecimal.ZERO) == 1)
        .collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);
    }

}